import { ajax, calcTimeHeader } from '../../utils/util.js'
let app = getApp()
Component({
  properties: {
    orderDetail: {
      type: Object,
      value: {}
    }
  },
  data: {
    showModal_goOrder: false
  },
  methods: {
    goOrderBtn () {
      this.setData({
        showModal_goOrder: true
      })
    },
    cancel () {
      this.setData({
        showModal_goOrder: false
      })
    },
    confirmOrder () {
      // 主播确定开始订单
      ajax('order/confirm_start', {
        token: wx.getStorageSync('Cookie'),
        order_no: this.data.orderDetail.order_no
      }).then(res => {
        let data = res.data || {}
        if (res.code == 0) {
          data.create_time = calcTimeHeader(data.create_time)
          data.plan_time = calcTimeHeader(data.plan_time)
          data.status = app.globalData['orderstatus'][data.status]
          this.setData({
            orderDetail: data
          })
        } else {
          wx.showToast({
            title: '网络开小差了',
            icon: 'none',
            duration: 1000
          })
        }
        this.setData({
          showModal_goOrder: true
        })
      })
    },
    refuseOrder () {
      // 主播拒绝开始订单
      ajax('order/confirm_start', {
        token: wx.getStorageSync('Cookie'),
        order_no: this.data.orderDetail.order_no
      }).then(res => {
        let data = res.data || {}
        if (res.code == 0) {
          data.create_time = calcTimeHeader(data.create_time)
          data.plan_time = calcTimeHeader(data.plan_time)
          data.status = app.globalData['orderstatus'][data.status]
          this.setData({
            orderDetail: data
          })
        }
      })
    },
    goOrder () {
      ajax('order/request_start_Sell', {
        token: wx.getStorageSync('Cookie'),
        order_no: this.data.orderDetail.order_no
      }).then(res => {
        let data = res.data || {}
        if (res.code == 0) {
          data.create_time = calcTimeHeader(data.create_time)
          data.plan_time = calcTimeHeader(data.plan_time)
          console.log(`订单状态:${data.status}`)
          data.status = app.globalData['orderstatus'][data.status]
          this.setData({
            orderDetail: data
          })
        } else {
          wx.showToast({
            title: '网络开小差了',
            icon: 'none',
            duration: 1000
          })
        }
        this.setData({
          showModal_goOrder: false
        })
      })
    }
  }
})
